package com.ssyx.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssyx.common.exception.SsyxException;
import com.ssyx.common.result.ResultCodeEnum;
import com.ssyx.model.sys.RegionWare;
import com.ssyx.sys.mapper.RegionWareMapper;
import com.ssyx.sys.service.RegionWareService;
import com.ssyx.vo.sys.RegionWareQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * ClassName:RegionWareServiceImpl
 * Description:
 *
 * @Author kay
 * @Create 2024/4/26 16:26
 * @Version 1.0
 */
@Service
public class RegionWareServiceImpl extends ServiceImpl<RegionWareMapper, RegionWare>
        implements RegionWareService {
    //开通区域列表
    @Override
    public IPage<RegionWare> getPageRegionWare(Page<RegionWare> pageParam, RegionWareQueryVo regionWareQueryVo) {
        //获取查询条件
        String keyword = regionWareQueryVo.getKeyword();

        //判断条件值是否为空,不为空时封装条件
        LambdaQueryWrapper<RegionWare> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)){
            //条件:根据区域名称,或者,仓库名称 查询
            wrapper.like(RegionWare::getRegionName,keyword)
                    .or()
                    .like(RegionWare::getWareName,keyword);
        }
        IPage<RegionWare> regionWarePage = baseMapper.selectPage(pageParam, wrapper);
        return regionWarePage;
    }


    //添加开通区域
    @Override
    public void saveRegionWare(RegionWare regionWare) {
        //判断区域是否已开通
        LambdaQueryWrapper<RegionWare> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionWare::getRegionId,regionWare.getRegionId());
        Long count = baseMapper.selectCount(wrapper);
        if(count>0){
            //已经存在
            throw new SsyxException(ResultCodeEnum.REGION_NO_OPEN);
        }
        baseMapper.insert(regionWare);
    }


    //取消开通区域
    @Override
    public void updateStatus(Long id, Integer status) {
        RegionWare regionWare = baseMapper.selectById(id);
        regionWare.setStatus(status);
        baseMapper.updateById(regionWare);
    }
}
